package cn.kongwg.demo02;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @author kongwg
 * @create 2021-05-26 13:49
 */
public class ZxDmeo {
    AtomicReference atomicInteger = new AtomicReference();

    public void mylock() {
        Thread thread = Thread.currentThread();
        System.out.println(Thread.currentThread().getName() + "\t come in ");
        while (!atomicInteger.compareAndSet(null, thread)) {
            //摸鱼
        }

    }

    public void myunlock() {
        Thread thread = Thread.currentThread();
        atomicInteger.compareAndSet(thread, null);
        System.out.println(Thread.currentThread().getName() + "\t unlock ");
    }

    public static void main(String[] args) {

        System.out.println(CzqdDemo.AUTUMN);
        System.out.println(CzqdDemo.AUTUMN.getId());
        System.out.println(CzqdDemo.AUTUMN.getName());
        System.out.println(CzqdDemo.values());
        System.out.println("---------------------");
        ZxDmeo zxDmeo = new ZxDmeo();
        new Thread(() -> {
            zxDmeo.mylock();
            try {TimeUnit.SECONDS.sleep(5);} catch (InterruptedException e) {e.printStackTrace(); }
            zxDmeo.myunlock();
        }, "t1").start();

        new Thread(() -> {
             try {TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}
             zxDmeo.mylock();
             zxDmeo.myunlock();
        }, "t2").start();
    }
}
